package com.foreknow.dao.impl;

import com.foreknow.bean.Admin;
import com.foreknow.dao.AdminDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by foreknow on 2022/6/27.
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public Admin login(String username, String password) {
        String sql = "select * from admin where username=? and password=?";
        //从工厂中获取AdminMapping对象
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,username,password);
        if(list.size() > 0){
            return  (Admin) list.get(0);
        }
        return null;
    }

    @Override
    public boolean register(Admin admin) throws SQLException {
        String sql = "insert into admin(username,password) values(?,?)";
        int i = jdbcTemplate.update(sql,admin.getUsername(),admin.getPassword());
        return i == 1;
    }
}
